package com.shuai.shuaisecondhand_springboot.controller;

import com.shuai.shuaisecondhand_springboot.common.Constants;
import com.shuai.shuaisecondhand_springboot.common.Result;
import com.shuai.shuaisecondhand_springboot.entity.Goods;
import com.shuai.shuaisecondhand_springboot.exception.ServiceException;
import com.shuai.shuaisecondhand_springboot.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    //查询
    @GetMapping("/getGoods")
    public List<Goods> getGoods() {
        return goodsService.findAll();
    }

    //根据uid查询数据
    @GetMapping("/getUidGoods")
    public Map<String, Object> getUidGoods(@RequestParam("Uid") String Uid, @RequestParam("status") String status, @RequestParam("name") String name) {

        List<Goods> data = goodsService.getUidGoods(Uid, status, name);
        Integer total = goodsService.getUidTotal(Uid, status, name);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    //根据id查询数据
    @GetMapping("/getOneGoods")
    public Goods findOne(@RequestParam("Gid") Integer Gid) {
        return goodsService.findOne(Gid);
    }

    //增加
    @PostMapping("/insertGoods")
    public Result addGoods(@RequestBody Goods goods) {
        log.warn("Insert");
        if (goodsService.insertGoods(goods)) {
            return Result.success("添加成功");
        } else {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }

    }

    //修改商品状态
    @PutMapping("/updateGoodsStatus")
    public Result updateGoodsStatus(@RequestParam("id") String id, @RequestParam("status") String status) {
        if (goodsService.updateGoodsStatus(id, status)) {
            return Result.success("交易成功");
        } else {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }
    }


    //管理员修改
    @PutMapping("/updateGoods")
    public Result updateGoods(@RequestBody Goods goods) {
        log.warn("updateGoods方法过来了。。。。。");
        if (goods.getAudit().equals("0")) {
            goods.setStatus("0");
        } else if (goods.getAudit().equals("1")) {
            goods.setStatus("1");
        }
        if (goodsService.updateGoods(goods)) {
            return Result.success("修改成功");
        } else {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }
    }

    //用户用户修改
    @PutMapping("/updateUserGoods")
    public Result updateUserGoods(@RequestBody Goods goods) {
        if (goodsService.updateUserGoods(goods)) {
            return Result.success("修改成功");
        } else {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }
    }

    //删除
    @DeleteMapping("/deleteGoods/{id}")
    public Integer deleteGoods(@PathVariable("id") Long id) {
        return goodsService.deleteGoods(id);
    }

    //批量删除
    @PostMapping("/deleteGoods/batch")
    public Integer deleteGoodsBatch(@RequestBody List<Integer> ids) {
        return goodsService.deleteGoodsBatch(ids);
    }

    //用户分页查询
    // 接口路径：/user/page?pageNum=1&pageSize=10
    // @RequestParam接受
    // limit第一个参数 = (pageNum - 1) * pageSize
    // pageSize
    @GetMapping("/getGoodsPage")
    public Map<String, Object> getGoodsPage(@RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize,
                                            @RequestParam String name) {
        pageNum = (pageNum - 1) * pageSize;
        List<Goods> data = goodsService.getGoodsPage(pageNum, pageSize, name);
        Integer total = goodsService.getTotal(name);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    //管理员分页查询
    // 接口路径：/user/page?pageNum=1&pageSize=10
    // @RequestParam接受
    // limit第一个参数 = (pageNum - 1) * pageSize
    // pageSize
    @GetMapping("/getAdminGoodsPage")
    public Map<String, Object> getAdminGoodsPage(@RequestParam Integer pageNum,
                                                 @RequestParam Integer pageSize,
                                                 @RequestParam String name) {
        pageNum = (pageNum - 1) * pageSize;
        List<Goods> data = goodsService.getAdminGoodsPage(pageNum, pageSize, name);
        Integer total = goodsService.getAdminTotal(name);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    //查询总商品数
    @GetMapping("/getGoodsAllTotal")
    public int getAllTotal() {
        return goodsService.getAllTotal();
    }
}
